/**
 * Copyright (c) Huawei Technologies Co., Ltd. 1987-2021. All rights reserved.
*/

package com.gitee.fufu669.aspect;

import com.gitee.fufu669.common.CacheKeyCommon;
import com.gitee.fufu669.utils.CacheJsonUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

// 注解
@Aspect
// 组件注解
@Component
// 注解
@Order(-69)
// 注解
@SuppressWarnings({"rawtypes"})
/** @author wangfupeng */
/**
 * TODO
*/
public class LogMethodAop {

    /**
     * TODO
    */
    public static final Logger logger = LoggerFactory.getLogger(LogMethodAop.class);

    // 注解
    @Around("@annotation(com.gitee.fufu669.aspect.LogMethod)")
    /**
     * TODO
    */
    public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 赋值
        int logBefore = CacheKeyCommon.DEFAULT_YES_VALUE;
        // 赋值
        int logAfter = CacheKeyCommon.DEFAULT_YES_VALUE;
        // 尝试下面的代码
        try {
            // 字符串赋值
            String targetName = proceedingJoinPoint.getTarget().getClass().getName();
            // 字符串赋值
            String methodName = proceedingJoinPoint.getSignature().getName();
            // TODO
            Class targetClass = Class.forName(targetName);
            // TODO
            Method[] methods = targetClass.getMethods();
            // 遍历
            for (Method method : methods) {
                // 判断
                if (method.getName().equals(methodName)) {
                    // TODO
                    logBefore = method.getAnnotation(LogMethod.class).logBefore();
                    // TODO
                    logAfter = method.getAnnotation(LogMethod.class).logAfter();
                    // 跳出
                    break;
                }
            }
        // 捕捉异常
        } catch (Exception e) {
            logger.info(e.toString());
        }
        // TODO
        Object target = proceedingJoinPoint.getTarget();
        /** 当前的类的全名*/
        String targetName = target.getClass().getName();
        /** 当前的方法名 */
        String methodName = proceedingJoinPoint.getSignature().getName();
        // 赋值
        String logString = "";
        // 增加
        logString += "\n%%%%%%%%%%%%%%%%%%%%%%%%%方法调用前%%%%%%%%%%%%%%%%%%%%%%%%%";
        // 增加
        logString += "\nclass       : " + targetName;
        // 增加
        logString += "\nmethod      : " + methodName;
        // TODO
        Object[] argumentsValues = proceedingJoinPoint.getArgs();
        // 赋值
        String parameterString = "\nparameter   : ";
        // 遍历
        for (int i = 0; i < argumentsValues.length; i++) {
            // 赋值
            Object argumentsValue = argumentsValues[i];
            // 尝试下面的代码
            try {
                // 增加
                parameterString += (i == 0 ? "" : ", ") + ("parameter" + (i + 1) + "=" + CacheJsonUtil.toJson(argumentsValue));
            // 捕捉异常
            } catch (Exception e) {
                // 增加
                parameterString += (i == 0 ? "" : ", ") + ("parameter" + (i + 1) + "=" + "参数无法序列化");
            }
        }
        // 增加
        logString += parameterString;
        // 增加
        logString += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
        // 判断
        if(logBefore==CacheKeyCommon.DEFAULT_YES_VALUE) {
            logger.info(logString);
        }
        // 赋值
        logString =  "\n%%%%%%%%%%%%%%%%%%%%%%%%%方法调用后%%%%%%%%%%%%%%%%%%%%%%%%%\n";
        // 增加
        logString += "class       : " + targetName;
        // 增加
        logString += "\nmethod      : " + methodName;
        // 增加
        logString += parameterString + "";
        // TODO
        Object value = proceedingJoinPoint.proceed();
        // 尝试下面的代码
        try {
            // 增加
            logString += ("\nreturn class: " + value.getClass());
        // 捕捉异常
        } catch (Exception e) {
            // 增加
            logString += ("\nreturn class: " + "结果没有类名");
        }
        // 尝试下面的代码
        try {
            // 增加
            logString += ("\nreturn value: " + CacheJsonUtil.toJson(value));
        // 捕捉异常
        } catch (Exception e) {
            // 增加
            logString += ("\nreturn value: " + "结果无法序列化" + value);
        }
        // 增加
        logString += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
        // 判断
        if(logAfter==CacheKeyCommon.DEFAULT_YES_VALUE) {
            logger.info(logString);
        }
        // 返回
        return value;
    }

}
